package com.k.generator;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DbUtil {
    private static Connection getConn(String url, String user, String password, String driver) {
        if(driver==null){
            throw new IllegalArgumentException("driver must not be null");
        }
        Connection conn = null;
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, user, password);
            System.out.println("GET CONN SUCCESS!");
            return conn;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    private static Connection getConn(String url, String user, String password, DbTypeEnum dbType) {
        String driver = "";
        if(DbTypeEnum.ORACLE == dbType){
            driver = "oracle.jdbc.driver.OracleDriver";
        } else if(DbTypeEnum.SQLSERVER == dbType){
            driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        } else if (DbTypeEnum.MYSQL == dbType){
            driver = "com.mysql.jdbc.Driver";
        } else if (DbTypeEnum.POSTGRES == dbType){
            driver = "org.postgresql.Driver";
        } else if (DbTypeEnum.SQLITE == dbType) {
            driver = "org.sqlite.JDBC";
        }
        return getConn(url,user,password,driver);
    }


    public static List<String> getAllTableNames(DbTypeEnum dbType, String url, String user, String password){
        List<String> allTableNames = new ArrayList<>();
        String querySql = "";
        if(DbTypeEnum.SQLITE == dbType){
            querySql = " SELECT name FROM sqlite_master WHERE type='table' ORDER BY name ";
        } else if(DbTypeEnum.POSTGRES == dbType){
            querySql = " SELECT tablename FROM pg_tables where schemaname = 'public' ";
        } else if(DbTypeEnum.ORACLE == dbType){
//            querySql = "select * from user_tables";
            querySql = " SELECT TABLE_NAME from all_tables where owner='SZYJ' ";
        } else if (DbTypeEnum.MYSQL == dbType){
            querySql = " SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'sop' ";
        }
        Connection conn = getConn(url,user,password,dbType);
        PreparedStatement ps = null;
        ResultSet rs=null;
        try {
            ps = conn.prepareStatement(querySql);
            rs = ps.executeQuery();
            while(rs.next()){
                String tableName = rs.getString(1);
                String tableNameLower = tableName.toLowerCase();
                allTableNames.add(tableNameLower);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            if(rs!=null){
                try {
                    rs.close();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
            if(ps!=null){
                try {
                    ps.close();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return allTableNames;
    }

    public static List<String> convertMybatisGenList(List<String> allTables){
        List<String> mybatisGeneratorList = new ArrayList<>();
        for(String tableName :allTables){
            String modelName = tableName.substring(0, 1).toUpperCase()+tableName.substring(1) ;
            //System.out.println("rsname:"+rsname+" convertName:"+convertName);
            String content = "<table tableName=\""+tableName+"\" domainObjectName=\""+modelName+"\" "
                    +"enableCountByExample=\"false\" enableSelectByExample=\"false\" "
                    +"enableDeleteByExample=\"false\" enableUpdateByExample=\"false\">"
                    +"<property name=\"useActualColumnNames\" value=\"false\" />"
                    + "</table>";
            System.out.println(content);
            mybatisGeneratorList.add(content);
        }
        return mybatisGeneratorList;
    }

    public static void main(String[] args) {
        DbTypeEnum dbType = DbTypeEnum.SQLITE;
        String schema = "";
        String url = "jdbc:sqlite:D:\\gitrepo\\asbe\\kpp\\src\\main\\resources\\data\\sqlite\\sql.db";
        String user = "";
        String password = "";
        List<String> allTableNames = getAllTableNames(dbType,url,user,password);
        List<String> mybatisGeneratorTableNames = convertMybatisGenList(allTableNames);
        for(String tableName:mybatisGeneratorTableNames){
            System.out.println(tableName);
        }
    }
}
